package com.atguigu.yygh.user.api;

import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.utils.AuthContextHolder;
import com.atguigu.yygh.model.user.UserInfo;
import com.atguigu.yygh.user.service.UserInfoService;
import com.atguigu.yygh.vo.user.LoginVo;
import com.atguigu.yygh.vo.user.UserAuthVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @ClassName UserInfoApiController
 * @Description: 类描述
 * @Author: admin
 * @CreateDate: 2024/6/18 15:44
 * @UpdateUser: 更新人
 * @UpdateDate: 2024/6/18 15:44
 * @UpdateRemark: 更新的信息
 * @Version: 1.0
 */

@RestController
@RequestMapping("/api/user")
public class UserInfoApiController {

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 用户通过手机号登录接口
     * @param loginVo 登录参数
     * @return
     */
    @PostMapping("login")
    public Result login(@RequestBody LoginVo loginVo) {
        Map<String, Object> remap=userInfoService.loginUser(loginVo);
        return Result.ok(remap);
    }

    /**
     * 用户实名认证接口
     * @param userAuthVo 实名认证参数
     * @param request 请求对象
     * @return
     */
    @PostMapping("auth/userAuth")
    public Result userAuth(@RequestBody UserAuthVo userAuthVo, HttpServletRequest request) {
        //传递两个参数，一个是用户id，另一个是认证数据vo对象
        userInfoService.userAuth(AuthContextHolder.getUserId(request), userAuthVo);
        return Result.ok();
    }

    /**
     * 获取用户信息接口，根据token中的用户id获取用户信息
     * @param request 请求对象
     * @return 用户信息
     */
    @GetMapping("auth/getUserInfo")
    public Result getUserInfo(HttpServletRequest request) {
        Long userId = AuthContextHolder.getUserId(request);
        UserInfo userInfo = userInfoService.getById(userId);
        return Result.ok(userInfo);
    }
}
